package com.fzu.mmdbproxyframework.configurationmanager;

import java.io.InputStream;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import com.fzu.mmdbproxyframework.context.ClusterContext;
import com.fzu.mmdbproxyframework.context.ConfigContext;
import com.fzu.mmdbproxyframework.mmdb.h2.H2DB;
import com.fzu.mmdbproxyframework.model.DBInterf;
import com.fzu.mmdbproxyframework.model.DataSource;
import com.fzu.mmdbproxyframework.model.MMDataNodeCluster;

public class MMDBConfigRead {
	public	MMDataNodeCluster cluster;
	
	public static void main(String[] args) {
		System.out.println("adfas");
	}
	  /**
	 * 
	 */
	private MMDBConfigRead() {
		super();
	}
	public MMDBConfigRead(MMDataNodeCluster cluster){
		  this.cluster = cluster;
	  }
	  /*MMDBParam参数说明
	   * 			name=mm_DB_1    内存数据库的名字 
	   * 			type=h2 默认h2  内存数据库的类型
	   * 			mode=MEM 内数据库模式   默认MEM
	   * 			dataSource_name=mm_ds_m 内存数据的数据源名字
	   * 			dataSource_type=master 内存数据的数据源类型  默认 master
	   * 			dataSource_dbType=h2    数据源的数据库类型默认  h2
	   * 			dataSource_dbMode=MEM 数据源的数据库模式默认  MEM
	   * tableNameList参数说明
	   * 			当alltables为false时候  这个配置起作用  里面是要同步的表名
	   * **/
	  public  List<DBInterf> buildMMDBConfig(Map<String,String> MMDBParam,List<String> tableNameList){
		  System.out.println("buildMMDBConfig +ClusterContext.getInstance().getMMDBNodeList().size()="+
				  cluster.getClusterContext().getMMDBNodeList().size());
		  for(int i=1;i<=cluster.getConfigContext().getMMDBNumbers();i++){
			  DBInterf DBNode = null;
			  if(MMDBParam.get("type").equalsIgnoreCase("h2")){
				  DBNode = new H2DB(cluster);
			  }else{
				  System.out.println("qita=-数据ku类型"); 
			  }
			  DBNode.setMode(MMDBParam.get("mode"));
			  DBNode.setDBNodeName(MMDBParam.get("name"));
			  DBNode.setDBNodeType(MMDBParam.get("type"));
			  
			  DataSource dataSource = new DataSource(cluster);
			  dataSource.setDataSourcename(MMDBParam.get("dataSource_name"));
			  dataSource.setType(MMDBParam.get("dataSource_type"));
			  dataSource.setDBType(MMDBParam.get("dataSource_dbType"));
			  dataSource.setDBmode(MMDBParam.get("dataSource_dbMode"));
			  dataSource.setShutdown(false);
			  
			  if(cluster.getConfigContext().getAllTables().equalsIgnoreCase("false")){
				  DBNode.setTableNameList(tableNameList);
			  }
			  
			  DBNode.getDataSourceList().add(dataSource);//添加多个dataSourceNodes信息
			  
			  cluster.getClusterContext().getMMDBNodeList().add(DBNode);//加入用户上下文环境
			  
		  }
		  System.out.println("buildMMDBConfig ClusterContext.getInstance().getMMDBNodeList()="+cluster.getClusterContext().getMMDBNodeList().size());
			return cluster.getClusterContext().getMMDBNodeList();
		 }  
	  
	  public  List<DBInterf> buildMMDBNodeList() {
	    	 SAXReader reader = new SAXReader(); 
	    	 InputStream in = MMDBConfigRead.class.getClassLoader().getResourceAsStream("h2database.xml");  
	         Document doc;
			try {
				doc = reader.read(in);
	             Element host = doc.getRootElement(); 
	             if (host == null) return null;
	             System.err.println(host.attributeValue("name"));
	             // all college node List<Element> childNodes = root.elements();  
	             List<Element> datanodeNodes = host.elements();
	             System.out.println("datanodeNodes.getLength()="+datanodeNodes.size());
//	             MMDBCluster.setVmNums(datanodeNodes.size());
	             for(int i = 0; i < datanodeNodes.size(); i++) {//添加多个datanode节点
	            	 H2DB h2DB = null ;//创建一个数据节点 datanode
	            	 Element datanode = datanodeNodes.get(i);
	                 if (datanode != null && datanode.getNodeType() == Node.ELEMENT_NODE) {
	                     System.err.println("\t" + datanode.attributeValue("name"));
	                     System.err.println("\t" + datanode.attributeValue("type"));
	                     System.err.println("\t mode=" + datanode.attributeValue("mode"));
	                     if(datanode.attributeValue("type").equalsIgnoreCase("h2")){
	                    	 h2DB = new H2DB(cluster);
	                     }
	                     h2DB.setMode(datanode.attributeValue("mode"));
	                     h2DB.setDBNodeName(datanode.attributeValue("name"));
	                     h2DB.setDBNodeType(datanode.attributeValue("type"));
	                     // all class node
	                     List<Element> datasourceNod = datanode.elements();
	                     for (int j = 0; j < datasourceNod.size(); j++) {//添加多个dataSourceNodes信息
	                    	 Element datasource = datasourceNod.get(j);
	                         DataSource dataSource = new DataSource(cluster);  
	                         if (datasource != null && datasource.getNodeType() == Node.ELEMENT_NODE) {
	                             System.err.println("\t\t" + datasource.attributeValue("name"));
	                             System.err.println(" " + datasource.attributeValue("type"));
	                             dataSource.setDataSourcename(datasource.attributeValue("name"));
	                             dataSource.setType(datasource.attributeValue("type"));
	                             dataSource.setDBType(datasource.attributeValue("DBType"));
	                             dataSource.setDBmode(datasource.attributeValue("DBmode"));
//	                             dataSource.setShutdown(false);//设置datasource是否可用
	                             // all student node
	                             List<Element> propertyNodes = datasource.elements();
	                             for (int k = 0; k < propertyNodes.size(); k++) {//为dataSourceNod添加多个表
	                            	 Element property = propertyNodes.get(k);
	                                 if (property != null && property.getNodeType() == Node.ELEMENT_NODE) {
	                                	String tbName = property.attributeValue("name");	                                
	                                	System.err.print("\t\t\t" + tbName);	
	                                	h2DB.getTableNameList().add(tbName);
	                                }
	                             }
	                             h2DB.getDataSourceList().add(dataSource);
	                         }    
	                     }
	                 }
	                 System.out.println("第"+i+"个MMdatanode");
	                 cluster.getClusterContext().getMMDBNodeList().add(h2DB);//加入上下文环境
	             }
	           return  cluster.getClusterContext().getMMDBNodeList();
			} catch (DocumentException e) {
				e.printStackTrace();
				return null;  
			}
	        
	    }
}
